﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Step4.Unit8.Modules;

namespace Step4.Unit8.Controllers
{
    // 不添加策略也生效，但是，为了保险起见，还是应该添加策略
    // 添加了策略，就可以不用添加角色列表
    [Authorize(Policy ="角色管理授权策略")] // 需要授权  如果需要设置多个角色，中间以逗号","隔开  (通过策略绑定了角色)
    // [Authorize(Roles ="管理员,普通用户")] 与上面的是等效的                                     (直接指定被授权的角色)
    public class RoleController : Controller    
    {
        private readonly RoleManager<IdentityRole> _roleManager;

        public RoleController(RoleManager<IdentityRole> roleManager)
        { 
            _roleManager = roleManager;
        }

        /// <summary>
        /// 列表视图
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            return View(_roleManager.Roles.ToList());
        }

        /// <summary>
        /// 添加角色视图
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> Create(RoleCreateViewModel model)
        {
          
            return View();
        }

        /// <summary>
        /// 添加提交
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> SubmitCreate(RoleCreateViewModel model)
        {
            if (ModelState.IsValid)
            {
                var role = new IdentityRole
                {
                    Name = model.RoleName
                };
                var result = await _roleManager.CreateAsync(role);
                if (result.Succeeded)
                {
                    return RedirectToAction("Index");
                }
            }
            return View("Create", model);
        }


        /// <summary>
        /// 修改视图
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Edit(string id)
        {
            var role = await _roleManager.FindByIdAsync(id);
            var viewmode = new RoleEditViewModel
            {
                Id = id,
                RoleName = role.Name
            };

            return View(viewmode);
        }

        /// <summary>
        /// 修改提交
        /// </summary>
        /// <param name="viewModel"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> SubmitEdit(RoleEditViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                var role = await _roleManager.FindByIdAsync(viewModel.Id);
                role.Name = viewModel.RoleName;
                var result = await _roleManager.UpdateAsync(role);
                if (result.Succeeded)
                {
                    return RedirectToAction("Index");
                }
                foreach (var err in result.Errors)
                {
                    ModelState.AddModelError("", err.Description);
                }
                return RedirectToAction("Edit", viewModel);
            }
            return View("Edit",viewModel);  // 模型验证失败，留在Edit页面
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<IActionResult> Delete(string Id)
        {
            var role = await _roleManager.FindByIdAsync(Id);
            await _roleManager.DeleteAsync(role);
            return RedirectToAction("Index");
        }

    }
}
